package com.jlrdb.DAO;

import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.appengine.api.datastore.Transaction;
import com.jlrdb.model.TvBroadcast;
import com.jlrdb.model.TvBroadcasts;

public class TvBroadcastDAOImpl implements TvBroadcastDAO {
	@Override
	public int addTvBroadcast(TvBroadcast tvBroadcast) {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Transaction txn = datastore.beginTransaction();
		Entity tvdata = new Entity("TvBroadcastData");
		tvdata.setProperty("geographic_region", tvBroadcast.getGeographic_Region());
		tvdata.setProperty("country", tvBroadcast.getCountry());
		tvdata.setProperty("dtt_standard", tvBroadcast.getDtt_Standard());
		tvdata.setProperty("dtt_status", tvBroadcast.getDtt_Status());
		tvdata.setProperty("analog_standard", tvBroadcast.getAnalog_Standard());
		tvdata.setProperty("aso_date_start", tvBroadcast.getAso_Date_Start());
		tvdata.setProperty("aso_date_completion", tvBroadcast.getAso_Date_Completion());
		tvdata.setProperty("aso_status", tvBroadcast.getAso_Status());
		tvdata.setProperty("dso_date_start", tvBroadcast.getDso_Date_Start());
		tvdata.setProperty("dso_date_completion", tvBroadcast.getDso_Date_Completion());
		tvdata.setProperty("dso_status", tvBroadcast.getDso_Status());
		tvdata.setProperty("next_transition_planned", tvBroadcast.getNext_Transition_Planned());
		tvdata.setProperty("standard_transition_into", tvBroadcast.getStandard_Transition_Into());
		tvdata.setProperty("status", tvBroadcast.getStatus());
		tvdata.setProperty("start_date", tvBroadcast.getStart_Date());
		tvdata.setProperty("notes", tvBroadcast.getNotes());
		tvdata.setProperty("status_confirmed", tvBroadcast.getStatus_Confirmed());
		tvdata.setProperty("last_date_verified", tvBroadcast.getLast_Date_Verified());
		tvdata.setProperty("video_format_supported", tvBroadcast.getVideo_Format_Supported());
		tvdata.setProperty("operators", tvBroadcast.getOperators());
		tvdata.setProperty("mux_details", tvBroadcast.getMux_Details());
		tvdata.setProperty("dtt_coverage", tvBroadcast.getDtt_Coverage());
		tvdata.setProperty("hd_present", tvBroadcast.getHd_Present());
		tvdata.setProperty("audio_formats", tvBroadcast.getAudio_Formats());
		tvdata.setProperty("frequency_bands", tvBroadcast.getFrequency_Bands());
		tvdata.setProperty("interactive_standard", tvBroadcast.getInteractive_Standards());
		tvdata.setProperty("channel_bandwidth", tvBroadcast.getChannel_Bandwidth());
		tvdata.setProperty("carrier_type", tvBroadcast.getCarrier_Type());
		tvdata.setProperty("modulation", tvBroadcast.getModulation());
		tvdata.setProperty("broadcast_authority", tvBroadcast.getBroadcast_Authority());
		tvdata.setProperty("itu_region", tvBroadcast.getItu_Region());
		tvdata.setProperty("broadcasters", tvBroadcast.getBroadcasters());
		tvdata.setProperty("source_of_reference", tvBroadcast.getSource_Of_Reference());
		datastore.put(tvdata);
		txn.commit();
		return 0;
	}

	@Override
	public TvBroadcast getThisBroadcast(long broadcastId) {
		TvBroadcast tvBroadcast = new TvBroadcast();
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Key key = KeyFactory.createKey("TvBroadcastData", broadcastId);
		Entity result;
		try
		{
			result = datastore.get(key);
			tvBroadcast.setGeographic_Region(result.getProperty("geographic_region").toString());
			tvBroadcast.setCountry(result.getProperty("country").toString());
			tvBroadcast.setDtt_Standard(result.getProperty("dtt_standard").toString().toString());
			tvBroadcast.setDtt_Status(result.getProperty("dtt_status").toString());
			tvBroadcast.setAnalog_Standard(result.getProperty("analog_standard").toString());
			tvBroadcast.setAso_Date_Start(result.getProperty("aso_date_start").toString());
			tvBroadcast.setAso_Date_Completion(result.getProperty("aso_date_completion").toString());
			tvBroadcast.setAso_Status(result.getProperty("aso_status").toString());
			tvBroadcast.setDso_Date_Start(result.getProperty("dso_date_start").toString());
			tvBroadcast.setDso_Date_Completion(result.getProperty("dso_date_completion").toString());
			tvBroadcast.setDso_Status(result.getProperty("dso_status").toString());
			tvBroadcast.setNext_Transition_Planned(result.getProperty("next_transition_planned").toString());
			tvBroadcast.setStandard_Transition_Into(result.getProperty("standard_transition_into").toString());
			tvBroadcast.setStatus(result.getProperty("status").toString());
			tvBroadcast.setStart_Date(result.getProperty("start_date").toString());
			tvBroadcast.setNotes(result.getProperty("notes").toString());
			tvBroadcast.setStatus_Confirmed(result.getProperty("status_confirmed").toString());
			tvBroadcast.setLast_Date_Verified(result.getProperty("last_date_verified").toString());
			tvBroadcast.setVideo_Format_Supported(result.getProperty("video_format_supported").toString());
			tvBroadcast.setOperators(result.getProperty("operators").toString());
			tvBroadcast.setMux_Details(result.getProperty("mux_details").toString());
			tvBroadcast.setDtt_Coverage(result.getProperty("dtt_coverage").toString());
			tvBroadcast.setHd_Present(result.getProperty("hd_present").toString());
			tvBroadcast.setAudio_Formats(result.getProperty("audio_formats").toString());
			tvBroadcast.setFrequency_Bands(result.getProperty("frequency_bands").toString());
			tvBroadcast.setInteractive_Standards(result.getProperty("interactive_standard").toString());
			tvBroadcast.setChannel_Bandwidth(result.getProperty("channel_bandwidth").toString());
			tvBroadcast.setCarrier_Type(result.getProperty("carrier_type").toString());
			tvBroadcast.setModulation(result.getProperty("modulation").toString());
			tvBroadcast.setBroadcast_Authority(result.getProperty("broadcast_authority").toString());
			tvBroadcast.setItu_Region(result.getProperty("itu_region").toString());
			tvBroadcast.setBroadcasters(result.getProperty("broadcasters").toString());
			tvBroadcast.setSource_Of_Reference(result.getProperty("source_of_reference").toString());
			tvBroadcast.setBroadcastId(result.getKey().getId());
		} catch (EntityNotFoundException e)
		{
			e.printStackTrace();
		}
		return tvBroadcast;
	}

	@Override
	public int updateTvBroadcast(TvBroadcast tvBroadcast, long broadcastId) {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Transaction txn = datastore.beginTransaction();
		Key key = KeyFactory.createKey("TvBroadcastData", broadcastId);
		Entity tvdata = null;
		try
		{
			tvdata = datastore.get(key);
		} catch (EntityNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// tvdata.setProperty("geographic_region",
		// tvBroadcast.getGeographic_Region());
		// tvdata.setProperty("country", tvBroadcast.getCountry());
		tvdata.setProperty("dtt_standard", tvBroadcast.getDtt_Standard());
		tvdata.setProperty("dtt_status", tvBroadcast.getDtt_Status());
		tvdata.setProperty("analog_standard", tvBroadcast.getAnalog_Standard());
		tvdata.setProperty("aso_date_start", tvBroadcast.getAso_Date_Start());
		tvdata.setProperty("aso_date_completion", tvBroadcast.getAso_Date_Completion());
		tvdata.setProperty("aso_status", tvBroadcast.getAso_Status());
		tvdata.setProperty("dso_date_start", tvBroadcast.getDso_Date_Start());
		tvdata.setProperty("dso_date_completion", tvBroadcast.getDso_Date_Completion());
		tvdata.setProperty("dso_status", tvBroadcast.getDso_Status());
		tvdata.setProperty("next_transition_planned", tvBroadcast.getNext_Transition_Planned());
		tvdata.setProperty("standard_transition_into", tvBroadcast.getStandard_Transition_Into());
		tvdata.setProperty("status", tvBroadcast.getStatus());
		tvdata.setProperty("start_date", tvBroadcast.getStart_Date());
		tvdata.setProperty("notes", tvBroadcast.getNotes());
		tvdata.setProperty("status_confirmed", tvBroadcast.getStatus_Confirmed());
		tvdata.setProperty("last_date_verified", tvBroadcast.getLast_Date_Verified());
		tvdata.setProperty("video_format_supported", tvBroadcast.getVideo_Format_Supported());
		tvdata.setProperty("operators", tvBroadcast.getOperators());
		tvdata.setProperty("mux_details", tvBroadcast.getMux_Details());
		tvdata.setProperty("dtt_coverage", tvBroadcast.getDtt_Coverage());
		tvdata.setProperty("hd_present", tvBroadcast.getHd_Present());
		tvdata.setProperty("audio_formats", tvBroadcast.getAudio_Formats());
		tvdata.setProperty("frequency_bands", tvBroadcast.getFrequency_Bands());
		tvdata.setProperty("interactive_standard", tvBroadcast.getInteractive_Standards());
		tvdata.setProperty("channel_bandwidth", tvBroadcast.getChannel_Bandwidth());
		tvdata.setProperty("carrier_type", tvBroadcast.getCarrier_Type());
		tvdata.setProperty("modulation", tvBroadcast.getModulation());
		tvdata.setProperty("broadcast_authority", tvBroadcast.getBroadcast_Authority());
		tvdata.setProperty("itu_region", tvBroadcast.getItu_Region());
		tvdata.setProperty("broadcasters", tvBroadcast.getBroadcasters());
		tvdata.setProperty("source_of_reference", tvBroadcast.getSource_Of_Reference());
		datastore.put(tvdata);
		txn.commit();
		return 0;
	}

	@SuppressWarnings("deprecation")
	@Override
	public TvBroadcasts viewTvBroadcastNew(TvBroadcast tvQuery) {
		TvBroadcasts tvBroadcasts = new TvBroadcasts();
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query tvBroadcastData = null;
		tvBroadcastData = new Query("TvBroadcastData").addSort("country", SortDirection.ASCENDING);
		if (tvQuery.getCountry() != null)
			if (!tvQuery.getCountry().isEmpty())
				tvBroadcastData.addFilter("country", Query.FilterOperator.EQUAL, tvQuery.getCountry());
		if (tvQuery.getGeographic_Region() != null)
			if (!tvQuery.getGeographic_Region().isEmpty())
				tvBroadcastData.addFilter("geographic_region", Query.FilterOperator.EQUAL, tvQuery.getGeographic_Region());
		if (tvQuery.getDtt_Standard() != null)
			if (!tvQuery.getDtt_Standard().isEmpty())
				tvBroadcastData.addFilter("dtt_standard", Query.FilterOperator.EQUAL, tvQuery.getDtt_Standard());
		if (tvQuery.getDtt_Status() != null)
			if (!tvQuery.getDtt_Status().isEmpty())
				tvBroadcastData.addFilter("dtt_status", Query.FilterOperator.EQUAL, tvQuery.getDtt_Status());
		if (tvQuery.getAnalog_Standard() != null)
			if (!tvQuery.getAnalog_Standard().isEmpty())
				tvBroadcastData.addFilter("analog_standard", Query.FilterOperator.EQUAL, tvQuery.getAnalog_Standard());
		if (tvQuery.getAso_Status() != null)
			if (!tvQuery.getAso_Status().isEmpty())
				tvBroadcastData.addFilter("aso_status", Query.FilterOperator.EQUAL, tvQuery.getAso_Status());
		if (tvQuery.getDso_Status() != null)
			if (!tvQuery.getDso_Status().isEmpty())
				tvBroadcastData.addFilter("dso_status", Query.FilterOperator.EQUAL, tvQuery.getDso_Status());
		if (tvQuery.getNext_Transition_Planned() != null)
			if (!tvQuery.getNext_Transition_Planned().isEmpty())
				tvBroadcastData.addFilter("next_transition_planned", Query.FilterOperator.EQUAL, tvQuery.getNext_Transition_Planned());
		if (tvQuery.getStandard_Transition_Into() != null)
			if (!tvQuery.getStandard_Transition_Into().isEmpty())
				tvBroadcastData.addFilter("standard_transition_into", Query.FilterOperator.EQUAL, tvQuery.getStandard_Transition_Into());
		if (tvQuery.getStatus() != null)
			if (!tvQuery.getStatus().isEmpty())
				tvBroadcastData.addFilter("status", Query.FilterOperator.EQUAL, tvQuery.getStatus());
		if (tvQuery.getStatus_Confirmed() != null)
			if (!tvQuery.getStatus_Confirmed().isEmpty())
				tvBroadcastData.addFilter("status_confirmed", Query.FilterOperator.EQUAL, tvQuery.getStatus_Confirmed());
		if (tvQuery.getHd_Present() != null)
			if (!tvQuery.getHd_Present().isEmpty())
				tvBroadcastData.addFilter("hd_present", Query.FilterOperator.EQUAL, tvQuery.getHd_Present());
		if (tvQuery.getInteractive_Standards() != null)
			if (!tvQuery.getInteractive_Standards().isEmpty())
				tvBroadcastData.addFilter("interactive_standard", Query.FilterOperator.EQUAL, tvQuery.getInteractive_Standards());
		List<Entity> results = datastore.prepare(tvBroadcastData).asList(FetchOptions.Builder.withDefaults());
		if (!results.isEmpty())
		{
			for (Entity result : results)
			{
				TvBroadcast tvBroadcast = new TvBroadcast();
				tvBroadcast.setGeographic_Region(result.getProperty("geographic_region").toString());
				tvBroadcast.setCountry(result.getProperty("country").toString());
				tvBroadcast.setDtt_Standard(result.getProperty("dtt_standard").toString().toString());
				tvBroadcast.setDtt_Status(result.getProperty("dtt_status").toString());
				tvBroadcast.setAnalog_Standard(result.getProperty("analog_standard").toString());
				tvBroadcast.setAso_Date_Start(result.getProperty("aso_date_start").toString());
				tvBroadcast.setAso_Date_Completion(result.getProperty("aso_date_completion").toString());
				tvBroadcast.setAso_Status(result.getProperty("aso_status").toString());
				tvBroadcast.setDso_Date_Start(result.getProperty("dso_date_start").toString());
				tvBroadcast.setDso_Date_Completion(result.getProperty("dso_date_completion").toString());
				tvBroadcast.setDso_Status(result.getProperty("dso_status").toString());
				tvBroadcast.setNext_Transition_Planned(result.getProperty("next_transition_planned").toString());
				tvBroadcast.setStandard_Transition_Into(result.getProperty("standard_transition_into").toString());
				tvBroadcast.setStatus(result.getProperty("status").toString());
				tvBroadcast.setStart_Date(result.getProperty("start_date").toString());
				tvBroadcast.setNotes(result.getProperty("notes").toString());
				tvBroadcast.setStatus_Confirmed(result.getProperty("status_confirmed").toString());
				tvBroadcast.setLast_Date_Verified(result.getProperty("last_date_verified").toString());
				tvBroadcast.setVideo_Format_Supported(result.getProperty("video_format_supported").toString());
				tvBroadcast.setOperators(result.getProperty("operators").toString());
				tvBroadcast.setMux_Details(result.getProperty("mux_details").toString());
				tvBroadcast.setDtt_Coverage(result.getProperty("dtt_coverage").toString());
				tvBroadcast.setHd_Present(result.getProperty("hd_present").toString());
				tvBroadcast.setAudio_Formats(result.getProperty("audio_formats").toString());
				tvBroadcast.setFrequency_Bands(result.getProperty("frequency_bands").toString());
				tvBroadcast.setInteractive_Standards(result.getProperty("interactive_standard").toString());
				tvBroadcast.setChannel_Bandwidth(result.getProperty("channel_bandwidth").toString());
				tvBroadcast.setCarrier_Type(result.getProperty("carrier_type").toString());
				tvBroadcast.setModulation(result.getProperty("modulation").toString());
				tvBroadcast.setBroadcast_Authority(result.getProperty("broadcast_authority").toString());
				tvBroadcast.setItu_Region(result.getProperty("itu_region").toString());
				tvBroadcast.setBroadcasters(result.getProperty("broadcasters").toString());
				tvBroadcast.setSource_Of_Reference(result.getProperty("source_of_reference").toString());
				tvBroadcast.setBroadcastId(result.getKey().getId());
				tvBroadcasts.addTvBroadcast(tvBroadcast);
			}
		}
		return tvBroadcasts;
	}
}
